<script>
import { getSearch, getWord } from "../api/search.js"
import { ref, reactive } from 'vue'
import { useRoute, useRouter } from "vue-router";
import { getCatalogueData } from "../api/liber.js";
export default {
  name: 'SearchView',
  setup() {

    let data = reactive({
      keyword: "",
      result: null,
      hisot: [],
      volumeId:null,
      listsFun: [
        {
          "resId": 12395646,
          "word": "九劫剑魔",
          "picUrl": "https://book.img.zhangyue01.com/idc_1/group61/M00/13/08/CmRaIV-ABWWERlSkAAAAAC4BSqc904661245.jpg?v=suBznPtb&t=CmRaIV-ABWU."
        },
        {
          "resId": 11719663,
          "word": "女总裁的桃运兵王",
          "picUrl": "https://book.img.zhangyue01.com/idc_1/group6/M00/11/4E/CmQUN1vRLquEM45KAAAAALw2u98516653078.jpg?v=kRyTZ0LW&t=CmQUN1znUhQ."
        },
        {
          "resId": 12385056,
          "word": "超越狂暴升级",
          "picUrl": "https://book.img.zhangyue01.com/idc_1/group61/M00/0A/FD/CmRabl926oyEHB-NAAAAAK21Aho746844703.jpg?v=YawZbTbS&t=CmRabl926ow."
        },
        {
          "resId": 12590186,
          "word": "剑帝：一剑破万法",
          "picUrl": "https://book.img.zhangyue01.com/idc_1/group61/M00/04/EC/CmQUOGFEbGWERSltAAAAANJlL90894944633.jpg?v=XrDDfKyf&t=CmQUOGFEbGU."
        },
      ]
    })

    if (localStorage.getItem("sear")) {
      data.hisot = JSON.parse(localStorage.getItem("sear"))
    }

    let lists = ref(null);
    getSearch().then(data => {
      lists.value = data.searchList
    })

    let words = ref(null);
    getWord().then(data => {
      words.value = data.books
      console.log(words.value);
    })

    //搜索关键字
    let Key = () => {
      data.result = []
      if (data.keyword != "") {
        words.value.filter((item) => {
          if (item.word.indexOf(data.keyword) != -1) {
            data.result.push(item)
          }
        })
      }
    }





    //历史记录
    let history = (id) => {
      let index = data.result.findIndex(item => item.resId == id);
      console.log(data.hisot);
      let arr = data.hisot
      arr.push(data.result[index])
      window.localStorage.setItem("sear", JSON.stringify(arr))


    }

    //获取历史记录
    let jilu = ref([])
    jilu.value = JSON.parse(localStorage.getItem("sear"))
    console.log(jilu.value);

    //清空
    let del = () => {
      localStorage.removeItem("sear", JSON.stringify(jilu.value));
      jilu.value = [];
    }


    let liberFun = (id) => {
      let params = {
        books: id,
        libers: "",
      };
      getCatalogueData(params)
        .then((item) => {
          // console.log(item.data.volumes[0].volumeId);
          console.log(item.data.volumes);
          data.volumeId = item.data.volumes[0].volumeId
          params.libers = item.data.volumes[0].chapters[0].chapterId;
        })
        .then(() => {
          router.push({
            path:"/page",
            query:{
              books:params.books,
              libers: params.libers,
              volumeId:data.volumeId
            }
          });
        });
    };

    //跳转方法
    let route = useRoute();
    let router = useRouter();
    console.log("route", route);
    console.log("router", router);

    let back = () => {
      router.back()
    }

    return {
      lists,
      back,
      words,
      Key,
      data,
      history,
      jilu,
      del,
      liberFun
    }
  },
}
</script>


<template>
  <div class="search">
    <div class="container">
      <div class="search-module">
        <span class="iconfont icon-arrow-left" @click="back"></span>
        <div class="input">
          <input type="text" placeholder="请输入你想要搜索的内容" v-model="data.keyword" @input="Key">
          <ul v-show="data.keyword">
            <li v-for="item in data.result" :key="item.resId" @click="history(item.resId);liberFun('3377676')"><span
                class="iconfont icon-sousuo"></span>{{ item.word }}</li>

          </ul>
        </div>
        <p>搜索</p>
      </div>
      <div class="search_history">
        <div class="history">
          <p>搜索历史</p>

          <span class="iconfont icon-shanchu1" @click="del"></span>
        </div>
        <div class="history_item">
          <div class="item" @click="liberFun('3377676')" v-for="item in jilu" :key="item.resId">{{ item.word }}</div>
        </div>
        <div class="faxian">
          <p>搜索发现</p>
          <div class="content" @click="liberFun('3377676')">
            <div class="cont" v-for="item in data.listsFun" :key="item.resId">{{item.word}}</div>
          </div>

        </div>
      </div>
      <div class="search_hot">
        <van-swipe :loop="false" :width="300" :show-indicators="false">
          <van-swipe-item v-for="item in lists" :key="item.id">
            <div class="hot_box">
              <h3>{{ item.name }}</h3>
              <div class="hot_content" @click="liberFun('3377676')" v-show="index != 0" v-for="(item, index) in item.body" :key="index">
                <div class="hot_item">
                  <strong class="cons">{{ index }}</strong>
                  <div class="right">
                    <div class="image">
                      <img :src="item.pic" alt="">
                    </div>
                    <div class="title">
                      <p>{{ item.name }}</p>
                      <span>{{ item.play }}书友再看</span>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </van-swipe-item>
        </van-swipe>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
@import "../assets/font-two/iconfont.css";

.search {
  width: 100%;
  position: relative;
  ;

  .search-module {
    margin-top: 15px;
    padding: 0 16px;
    display: flex;
    justify-content: space-between;
    align-items: center;

    .input {
      width: 270px;
      height: 35px;
      background-color: #f7f7f7;
      border-radius: 20px;

      input {
        width: 100%;
        height: 100%;
        border: none;
        padding-left: 15px;
        font-size: 14px;

      }

      ul {
        position: absolute;
        left: 0;
        top: 40px;
        width: 100%;
        background-color: #fff;
        z-index: 100;
        height: 100%;

        li {
          padding: 10px 20px;
          font-size: 14px;
          display: flex;
          align-items: center;

          span {
            margin-right: 15px;
            color: #a9a9a9;
          }
        }
      }
    }

    p {
      font-size: 14px;
    }

  }

  .search_history {
    margin-top: 25px;
    padding: 0 16px;

    .history {
      display: flex;
      justify-content: space-between;
      align-items: center;

      p {
        font-weight: 600;
        font-size: 16px;
      }
    }

    .history_item {
      display: flex;
      flex-wrap: wrap;
      margin-top: 20px;
      padding-left: 10px;

      .item {
        font-size: 13px;
        background-color: #f4f4f4;
        color: #a9a9a9;
        border-radius: 15px;
        padding: 5px 10px;
        margin-right: 10px;
        margin-bottom: 15px;
      }
    }

    .faxian {
      margin-top: 30px;

      p {
        font-weight: 600;
      }

      .content {
        padding-left: 15px;

        .cont {
          width: 50%;
          display: inline-flex;
          font-size: 13px;
          margin-top: 20px;
          white-space: nowrap;
          overflow: hidden;
          text-overflow: ellipsis;
        }
      }


    }
  }

  .search_hot {
    margin-top: 30px;
    padding: 0 16px;

    .hot_box {
      margin-right: 15px;

      padding: 18px 5px;
      background-color: #f9f9f6;
      border-radius: 10px;
      padding-left: 10px;
      margin-bottom: 20px;

      h3 {
        font-weight: 600;
      }

      .hot_content {
        .hot_item {
          display: flex;
          justify-content: space-evenly;
          align-items: center;
          margin-top: 25px;

          .cons {
            color: #c8c0a8;
          }

          .right {
            display: flex;
            margin-left: 10px;

            .image {
              width: 36px;
              height: 36px;
              background-color: aqua;
              border-radius: 6px;
              overflow: hidden;

              img {
                width: 100%;
                height: 100%;
              }
            }

            .title {
              margin-left: 10px;


              p {
                font-size: 14px;
                width: 196px;
                white-space: nowrap;
                overflow: hidden;
                text-overflow: ellipsis;

              }

              span {
                color: #b3b3b0;
                font-size: 10px;
                margin-top: 8px;
              }
            }
          }
        }
      }
    }
  }
}
</style>